import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { CommitDocumentType } from '../schema/Commit/index';
import { AssemblyDocumentType } from '../schema/ClientHome/home.schema';
import { LogDocumentType } from '../schema/Commit/index';
import { AidDocumentType } from '../schema/Commit/index';
@Injectable()
export class CommitService {
  constructor(
    @InjectModel('Commit') private CommitModel: Model<CommitDocumentType>,
    @InjectModel('Assembly') private AssemblyModel: Model<AssemblyDocumentType>,
    @InjectModel('Log') private LogModel: Model<LogDocumentType>,
    @InjectModel('Aid') private AidModel: Model<AidDocumentType>,
  ) {}

  //保存评论
  async saveCommit(body: any) {
    const commit = new this.CommitModel(body);
    commit.save();
    return {
      code: 200,
      msg: '评论成功',
    };
  }

  //修改评论
  async updateCommit(body: any) {
    try {
      const { parentId } = body; // 假设 parentId 是评论的唯一标识符

      // 使用 parentId 作为查询条件，并将更新的字段应用到找到的评论中
      const updatedComment = await this.CommitModel.findOneAndUpdate(
        { parentId },
        body,
        { new: true },
      );

      if (!updatedComment) {
        return {
          code: 404,
          msg: '评论未找到',
        };
      }

      return {
        code: 200,
        msg: '评论更新成功',
        updatedComment,
      };
    } catch (error) {
      return {
        code: 500,
        msg: '评论更新失败',
        error: error.message,
      };
    }
  }

  //通过type和id获取评论
  async getCommit(query: any) {
    const { type, assId, userId } = query;

    const filter: any = {};

    if (type) {
      filter.type = type;
      if (assId) {
        filter.assId = assId;
      }
      if (userId) {
        filter.userId = userId;
      }
    }

    const result = await this.CommitModel.find(filter).sort({ time: -1 }); // -1表示降序排列

    if (!type && !userId) {
      // 请求全部数据
      const total = await this.CommitModel.find().countDocuments();
      return {
        code: 200,
        msg: 'success',
        data: total,
      };
    }

    return {
      code: 200,
      data: result,
    };
  }

  //上传配置
  async uploadConfig(body: any) {
    const config = new this.AssemblyModel(body);
    config.save();
    return {
      code: 200,
      msg: '上传成功',
    };
  }

  //获取配置
  async getConfig(query: any) {
    // 解构赋值，提取 id 和 userId
    const { id, userId } = query;

    // 构建查询条件
    const condition: any = {};
    if (id) condition._id = id;
    if (userId) condition.userId = userId;

    // 执行查询
    const result =
      userId || id
        ? await this.AssemblyModel.find(condition)
        : await this.AssemblyModel.find();

    // 返回结果
    return {
      code: 200,
      data: {
        result,
        total: await this.AssemblyModel.countDocuments(condition),
      },
    };
  }

  //上传LOG
  async uploadLog(body: any) {
    const res = new this.LogModel(body);
    console.log(res);

    res.save();
    return {
      code: 200,
      msg: '上传成功',
    };
  }

  //获取LOG
  async getLog() {
    const result = await this.LogModel.find();
    return {
      code: 200,
      data: result,
    };
  }

  //上传aid
  async uploadHelp(body: any) {
    const res = new this.AidModel(body);
    // console.log(res);
    res.save();
    return {
      code: 200,
      msg: '上传成功',
    };
  }

  //获取aid
  async getHelp() {
    const result = await this.AidModel.find();
    return {
      code: 200,
      data: result,
    };
  }
}
